package com.heisen.gateway.configure;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.util.pattern.PathPatternParser;


/**
 * 跨域设置
 */
@Configuration
public class GateWayCorsConfigure {


    /**
     *  跨域配置
     * @return
     */
    @Bean
    public CorsWebFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
        CorsConfiguration cors = new CorsConfiguration();
        cors.setAllowCredentials(true);   // 是否允许携带凭证（如 cookies）
        cors.addAllowedOriginPattern(CorsConfiguration.ALL);  // 设置允许访问的网络
        cors.addAllowedHeader(CorsConfiguration.ALL);  //允许所有请求头
        cors.addAllowedMethod(CorsConfiguration.ALL);  // 允许所有请求方法
        source.registerCorsConfiguration("/**", cors);  // 对所有路径应用 CORS 配置
        return new CorsWebFilter(source);

    }
}
